package com.cskaoyan.service;

import com.cskaoyan.bean.Issue;
import com.cskaoyan.bean.IssueExample;
import com.cskaoyan.bo.IssueCreateBO;
import com.cskaoyan.mapper.IssueMapper;
import com.cskaoyan.vo.IssueListVO;
import com.cskaoyan.vo.IssueVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CommonServiceImpl implements CommonService {

    @Autowired
    IssueMapper issueMapper;
    @Override
    public IssueVO queryIssue(Integer page, Integer limit, String question, String sort, String order) {
        PageHelper.startPage(page, limit);

        IssueExample issueExample = new IssueExample();

        issueExample.getOrderByClause(sort+ " "+order);

        IssueExample.Criteria criteria = issueExample.createCriteria().andDeletedEqualTo(false);

        if(question!=null&&!"".equals(question)){
            criteria.andQuestionLike("%" + question + "%");
        }

        List<Issue> issues = issueMapper.selectByExample(issueExample);

        PageInfo<Issue> issuePageInfo = new PageInfo<>();

        long total = issuePageInfo.getTotal();

        IssueVO issueVO = new IssueVO();

        issueVO.setItems(issues);

        issueVO.setTotal(total);

        return issueVO;
    }
    @Override
    public void issueDelete(Issue issue) {
            issue.setUpdateTime(new Date());
            issue.setDeleted(true);
            issueMapper.updateByPrimaryKeySelective(issue);
    }
    @Override
    public Issue issueUpdate(Issue issue) {
        issue.setUpdateTime(new Date());
        issue.setAnswer(issue.getQuestion());
        issue.setQuestion(issue.getAnswer());
        issueMapper.updateByPrimaryKeySelective(issue);
        return issue;
    }

    @Override
    public IssueListVO.ItemsBean create(IssueCreateBO issueCreateBO) {
        Issue issue = new Issue();
        issue.setQuestion(issueCreateBO.getQuestion());
        issue.setAnswer(issueCreateBO.getAnswer());
        Date time = new Date(System.currentTimeMillis());
        issue.setAddTime(time);
        issue.setUpdateTime(time);
        int i = issueMapper.insertSelective(issue);

        Issue newissue = issueMapper.selectByPrimaryKey(issue.getId());
        IssueListVO.ItemsBean itemsBean = new IssueListVO.ItemsBean();
        itemsBean.setId(newissue.getId());
        itemsBean.setQuestion(newissue.getQuestion());
        itemsBean.setAnswer(newissue.getAnswer());
        itemsBean.setAddTime(newissue.getAddTime().toString());
        itemsBean.setUpdateTime(newissue.getUpdateTime().toString());

        return itemsBean;
    }

   /* @Override
    public Issue issueCreate(Issue issue) {
        issue.setAddTime(new Date());
        issue.setDeleted(false);
        issue.setAnswer(issue.getQuestion());
        issue.setQuestion(issue.getAnswer());
        int i = issueMapper.insertSelective(issue);
        return issue;
    }*/
}
